package cs5226.actions;

import java.awt.Color;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

import cs5226.dao.BufferCacheDAO;
import cs5226.dao.ConfigurationDAO;
import cs5226.dao.MemorySortDAO;
import cs5226.dao.RedoLogDAO;
import cs5226.dao.RollbackSegmentDAO;
import cs5226.dao.SharePoolDAO;
import cs5226.model.AwrSetting;
import cs5226.model.PercentageSingleValueStatistics;
import cs5226.model.SingleValueStatistics;
import cs5226.model.ThresholdBean;
import cs5226.tags.ThresholdUtil;
import cs5226.util.StringUtil;

public class HomeAction extends ActionSupport implements
		ServletRequestAware {
	private HttpServletRequest request = null;

	
	public String execute () throws Exception {
		try {
			List<SingleValueStatistics> libraryStatList = SharePoolDAO.searchLibraryCacheStatisticsLatest();			
			List<SingleValueStatistics> dictStatList = SharePoolDAO.searchDictionaryCacheStatisticsLatest();			
			List<PercentageSingleValueStatistics> spFreeList = SharePoolDAO.searchSharedPoolFreeStatisticsLatest();
			
			request.setAttribute(ThresholdUtil.LIBRARY_CACHE_HIT_RATIO, libraryStatList);
			request.setAttribute(ThresholdUtil.DICTIONARY_CACHE_HIT_RATIO, dictStatList);	
			request.setAttribute(ThresholdUtil.SHARED_POOL_FREE, spFreeList);	
			
			
			List<SingleValueStatistics> bcList = BufferCacheDAO.getBufferCacheStatisticsLatest();	
			request.setAttribute(ThresholdUtil.BUFFER_CACHE_HIT_RATIO, bcList);
			
			
			List<PercentageSingleValueStatistics> ahList = RedoLogDAO.getRedoAllocationHitLatest();			
			List<SingleValueStatistics> rlsiwList = RedoLogDAO.getRedoLogSwitchIncompleteWaitLatest();
			
			request.setAttribute(ThresholdUtil.REDO_BUFFER_ALLOCATION_HIT, ahList);	
			request.setAttribute(ThresholdUtil.REDO_LOG_SWITCH_INCOMPLETE_CHECKPOINT_WAIT_TIME, rlsiwList);	
			
			
			List<PercentageSingleValueStatistics> msrList = MemorySortDAO.getMemorySortRatioLatest();					
			request.setAttribute(ThresholdUtil.MEMORY_SORT_PCT, msrList);
			
			
			List<PercentageSingleValueStatistics> rbpList = RollbackSegmentDAO.getRollbackPercentageLatest();				
			List<SingleValueStatistics> rbsecList = RollbackSegmentDAO.getRollbackPerSecondLatest();	
			
			request.setAttribute(ThresholdUtil.ROLLBACK_PCT, rbpList);
			request.setAttribute(ThresholdUtil.ROLLBACK_PER_SECOND, rbsecList);	
			
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
		}

		return super.SUCCESS;
	}
	

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
}
